package com.example.car.mapper;

import com.example.car.entity.Car;
import com.example.car.entity.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class CarMapper {

    @Autowired
    private MongoTemplate mongoTemplate;


    public List<Car> getCarList(Integer page, Integer limit) {
        List<Car> list = mongoTemplate.find(new Query(Criteria.where("status").is(0)).skip(page - 1).limit(limit), Car.class);
        return list;
    }

    public Car getCarById(String carId) {

        return mongoTemplate.findOne(new Query(Criteria.where("_id").is(carId)), Car.class);
    }

    public void rentCar(String carId) {
        Update update = new Update();
        update.set("status", 1);
        mongoTemplate.updateFirst(Query.query(Criteria.where("_id").is(carId)), update, Car.class);
    }
}
